//
//  RHMePricePatternCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/24.
//

#import "RHMePricePatternCell.h"
#import "RHPatternModel.h"

@interface RHMePricePatternCell()
@property(nonatomic, strong) QMUIButton *selectBtn;

@property(nonatomic, strong) UIImageView *policyIcon;

@property(nonatomic, strong) QMUILabel *policyNameLab;

@property(nonatomic, strong) QMUILabel *addressLab;

@property(nonatomic, strong) UIImageView *arrowImg;

@end
@implementation RHMePricePatternCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}


-(void)setPatternModel:(RHPatternModel *)patternModel
{
    _patternModel = patternModel;
    self.policyNameLab.text = ValidNullData(patternModel.templateName);
    self.selectBtn.selected = patternModel.isSelected;
    
    if (patternModel.cityName) {
        self.addressLab.text = ValidNullData(patternModel.cityName);
    }else{
        self.addressLab.text = ValidNullData(patternModel.stateName);
    }
    
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    UIView *bgView = [[UIView alloc]init];
    [self.contentView addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    [bgView rounded:8 width:1 color:kBorderColor];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 10, 5, 10));
        make.height.offset(80);
    }];
    
    _selectBtn= [[QMUIButton alloc]init];
    [_selectBtn setImage:[UIImage imageNamed:@"org_normal_icon"] forState:UIControlStateNormal];
    [_selectBtn setImage:[UIImage imageNamed:@"org_selected_icon"] forState:UIControlStateSelected];
    [_selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_selectBtn];
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(5);
        make.width.height.offset(30);
        make.centerY.mas_offset(0);
    }];
    
    
    _policyIcon = [[UIImageView alloc]init];
    [_policyIcon setImage:[UIImage imageNamed:@"me_policy_mine_icon"]];
    if ([self.reuseIdentifier isEqualToString:@"RHMePolicyPatternCell_mine"]) {
        [_policyIcon setImage:[UIImage imageNamed:@"me_policy_common_icon"]];
    }
    _policyIcon.contentMode = UIViewContentModeScaleAspectFill;
    [bgView addSubview:_policyIcon];
    [_policyIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.selectBtn.mas_right).offset(5);
            make.width.height.offset(24);
            make.top.mas_offset(10);
    }];
    
    _arrowImg = [[UIImageView alloc]init];
    [bgView addSubview:_arrowImg];
    [_arrowImg setImage:[UIImage imageNamed:@"arrow"]];
    [_arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(15);
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    _policyNameLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    [bgView addSubview:_policyNameLab];
    [_policyNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.policyIcon.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.policyIcon);
        make.right.equalTo(weakSelf.arrowImg.mas_left).offset(-3);
    }];

    
    
    QMUILabel *addressTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    addressTitleLab.text = @"城市:";
    [bgView addSubview:addressTitleLab];
    [addressTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.policyIcon).offset(0);
        make.bottom.mas_offset(-10);
        
    }];

    _addressLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    [bgView addSubview:_addressLab];
    [_addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addressTitleLab.mas_right).offset(3);
        make.centerY.equalTo(addressTitleLab);
        make.right.equalTo(weakSelf.arrowImg.mas_left).offset(-3);
    }];
}


-(void)selectAction:(QMUIButton*)btn
{
    self.patternModel.isSelected = !self.patternModel.isSelected;
    btn.selected = self.patternModel.isSelected;
    if (self.selectBlock) {
        self.selectBlock();
    }
}
@end
